//
//  MSSelectRemindViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/18.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSSelectRemindViewController.h"
#import "MSSelectRemindTableVC.h"

@interface MSSelectRemindViewController ()
@property (nonatomic, copy) NSString         *currentRemindFile;
@property (nonatomic, strong) NSFileManager  *fileManager;
@property (nonatomic, strong) NSMutableArray *stateArray;
@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSArray        *currentCodeArray;
@property (nonatomic, strong) MSSelectRemindTableVC *tableVC;
@end

@implementation MSSelectRemindViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = self.remindtype == RemindType_Change? @"老客户变动提醒":@"关注及跟踪的项目更新提醒";
    
    rightBarBtn.hidden = NO;
    [rightBarBtn setTitle:@"保存" forState:UIControlStateNormal];
}

- (void)initUI{
    self.tableVC = [[MSSelectRemindTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
}

- (void)testDataFunc{
    NSArray *changeCodeArray = @[@"trackProjectContactsAdd",@"followProjectContactsAdd",@"phoneBookContactsProject"];
    NSArray *updateCodeArray = @[@"projectDealStatusUpdate",@"projectFootprint",@"projectDynamics",@"projectContacts"];
    _currentCodeArray = self.remindtype == RemindType_Change? changeCodeArray:updateCodeArray;
    
    NSArray *changeArray = @[@"跟踪项目的所有客户联系人",@"关注项目的所有客户联系人",@"手机通讯录中的所有客户联系人"];
    NSArray *updateArray = @[@"交易更新提醒",@"足迹更新提醒",@"进展更新提醒",@"联系人更新提醒"];
    NSArray *tempArray = self.remindtype == RemindType_Change? changeArray:updateArray;
    
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString* changeRemindFile = [documentsPath stringByAppendingPathComponent:@"changeRemindFile.plist"];
    NSString* updateRemindFile = [documentsPath stringByAppendingPathComponent:@"updateRemindFile.plist"];
    self.currentRemindFile = self.remindtype == RemindType_Change? changeRemindFile:updateRemindFile;
    
    self.fileManager = [NSFileManager defaultManager];
    if ([self.fileManager fileExistsAtPath:self.currentRemindFile]) {
        self.stateArray = [[NSMutableArray alloc] initWithContentsOfFile:self.currentRemindFile];
    }
    else{
        self.stateArray = [[NSMutableArray alloc] initWithCapacity:0];
        for (int i=0; i<tempArray.count; i++) {
            [self.stateArray addObject:@"n"];
        }
    }
    self.tableVC.stateArray = self.stateArray;
    self.tableVC.dataArray = [tempArray mutableCopy];
}

- (void)rightBarBtnAction{
    NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *codeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i=0; i<self.stateArray.count; i++) {
        NSString *stateStr = self.stateArray[i];
        if ([stateStr isEqualToString:@"y"]) {
            [relutArray addObject:self.tableVC.dataArray[i]];
            [codeArray addObject:_currentCodeArray[i]];
        }
    }
    _titleArray = relutArray;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:codeArray options:NSJSONWritingPrettyPrinted error:nil];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    if (self.remindtype == RemindType_Change) {//联系人
        [netEngine requestContactsChangeWithTarget:self.VctrID hudView:self.contentView params:dic];
    }
    else{//项目更新
        [netEngine requestProjectUpdateWithTarget:self.VctrID hudView:self.contentView params:dic];
    }
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"ContactsChange"]!=nil) {
            [self saveSuccess];
        }
        if (responseDic[@"ProjectUpdate"]!=nil) {
            [self saveSuccess];
        }
    }
}

- (void)saveSuccess{
    if (self.ChoicBlock) {
        self.ChoicBlock(_titleArray);
    }
    [HDUtil showMessage:@"设置成功"];
    [rootNavigation popViewControllerAnimated:YES];
    [self.stateArray writeToFile:self.currentRemindFile atomically:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
